﻿using CNav.Business.Services;
using CNav.Domain.Admin;
using System.Web.Mvc;
using CNav.Support;
using WebGrease.Css.Extensions;

namespace CNav.Controllers
{
    [Authorize]
    [AdminAuthorize]
    public class DataValueController : BaseController
    {
        public ActionResult Index()
        {
            var dataValueList = Service.Get<IUiService>().GetDataValueList();
            return View(dataValueList.Data);
        }

        public ActionResult Data(int id)
        {
            var dataValueList = Service.Get<IUiService>().GetDataValueByDataList(id);
            return View(dataValueList.Data);
        }

        public ActionResult Details(int id = 0)
        {
            var response = Service.Get<IUiService>().GetDataValueById(id);
            if (response.Data == null)
            {
                return View("NotFound");
            }
            return View(response.Data);
        }

        public ActionResult Create(int id = 0)
        {
            if (id == 0)
                return RedirectToRoute("Home");

            DataValue dataValue = new DataValue();
            var dvsResponse = Service.Get<IUiService>().GetDataSourceById(id);
            dataValue.DataSourceName = dvsResponse.Data.Name;
            dataValue.DataSourceID = dvsResponse.Data.ID;
            return View(dataValue);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(DataValue dataValue)
        {
            if (ModelState.IsValid)
            {
                Service.Get<IUiService>().AddDataValue(dataValue);
                return new RedirectResult("~/DataSource/");
            }

            var dvsResponse = Service.Get<IUiService>().GetDataSourceById(dataValue.DataSourceID);
            dataValue.DataSourceName = dvsResponse.Data.Name;
            dataValue.DataSourceID = dvsResponse.Data.ID;

            return View(dataValue);
        }

        public ActionResult Edit(int id = 0)
        {
            var response = Service.Get<IUiService>().GetDataValueById(id);
            var datavalue = response.Data;
            if (datavalue == null)
            {
                return View("NotFound");
            }

            return View(datavalue);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(DataValue datavalue)
        {
            if (ModelState.IsValid)
            {
                var response = Service.Get<IUiService>().UpdateDataValue(datavalue);
                if (response.Error == null)
                    return RedirectToAction("Index");

                response.Error.ForEach(e => ModelState.AddModelError("", e));
            }

            return View(datavalue);
        }

        public ActionResult Delete(int id = 0)
        {
            var responseData = Service.Get<IUiService>().GetDataValueById(id);
            if (responseData.Data == null)
            {
                return View("NotFound");
            }
            return View(responseData.Data);
        }

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            Service.Get<IUiService>().DeleteDataValue(id);
            return RedirectToAction("Index");
        }
    }
}